﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using App.Data;
using App.Core.Extensions;

public partial class UserControls_LeftMenu : BaseUserControl
{
    private int _SelectedBusinessTypeID = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        _SelectedBusinessTypeID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.BUSINESS_TYPE_ID);
        BindLeftMenu();
    }
    protected void BindLeftMenu()
    {
        String itemsHtml = @"   <div class='LeftMenuBox'>
                                    <div class='LeftMenuBoxHeader'>{0}</div>
                                    <div style='background-color:#FFFFFF; padding-bottom: 7px;'>
                                        <ul class='LeftMenu'>
                                            {1}
                                        </ul>
                                    </div>
                                </div>";
        String lastItemHtml = @"<div class='LeftMenuBox' style='margin-bottom:0px;'>
                                    <div class='LeftMenuBoxHeader'>{0}</div>
                                    <div style='background-color:#FFFFFF; padding-bottom: 7px;'>
                                        <ul class='LeftMenu'>
                                            {1}
                                        </ul>
                                    </div>
                                </div>";

        ///Exclude Developer, Individual, And Load Providers for Service Provider Links
        BtcHousingEntities context = new BtcHousingEntities();
        var parentBusinessTypes = (from P in context.BusinessTypes where P.ParentID == null && ConfigReader.ServiceProviderBusinessTypeIDs.Contains(P.ID) select P).OrderBy(O => O.SortOrder).ToList();
        StringBuilder output = new StringBuilder(100);
        for (int i = 0; i < parentBusinessTypes.Count; i++ )
        {
            BusinessType parentBusinessType = parentBusinessTypes[i];
            StringBuilder sb = new StringBuilder(10);
            foreach (BusinessType childBusinessType in parentBusinessType.ChildBusinessTypes)
            {
                sb.AppendFormat("<li{3}><a href='/Pages/Public/CompanyList.aspx?{0}={1}'>{2}</a></li>", 
                    AppConstants.QueryString.BUSINESS_TYPE_ID
                    , childBusinessType.ID
                    , childBusinessType.ShortName.HtmlEncode()
                    , childBusinessType.ID == _SelectedBusinessTypeID ? " class='Current'" : String.Empty);
            }
            if(i == parentBusinessTypes.Count - 1)
                output.AppendFormat(lastItemHtml, parentBusinessType.ShortName.HtmlEncode(), sb.ToString());    
            else
                output.AppendFormat(itemsHtml, parentBusinessType.ShortName.HtmlEncode(), sb.ToString());
        }
        ltrList.Text = output.ToString();
    }
}